package scene;


// StereoGL.java
// Andrew Davison, June 2007, ad@fivedots.coe.psu.ac.th

/* Create a stereo view of a 3D world.

   The window stays centered in the middle of the screen when
   resized.

   Usage:
     runGL StereoGL [-x]

   The "-x" switches the left and right views of the scene, so they
   can be looked at with crossed eyes. The default is intended for
   parallel eye viewing.
*/

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.media.opengl.*;


public class StereoGL extends JFrame implements WindowListener
{
  private static int DEFAULT_FPS = 80;

  private static final int PWIDTH = 540;   // size of panel
  private static final int PHEIGHT = 330;


  private StereoCanvasGL canvas;


  public StereoGL(boolean isCrossed)
  {
    super("StereoGL");
    if (isCrossed)
      setTitle("StereoGL -x");

    Container c = getContentPane();
    c.setLayout( new BorderLayout() );
    c.add(makeRenderPanel(isCrossed), BorderLayout.CENTER);

    addWindowListener(this);

    pack();
    centerWindow();
    setVisible(true);
  } // end of StereoGL()


  private void centerWindow()
  // center the window in the middle of the screeen
  {
    Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension winDim = this.getSize();
    this.setLocation( (screenDim.width-winDim.width)/2,
                      (screenDim.height-winDim.height)/2);
  }  // end of centerWindow()



  private JPanel makeRenderPanel(boolean isCrossed)
  // construct the canvas
  {
    JPanel renderPane = new JPanel();
    renderPane.setLayout( new BorderLayout() );
    renderPane.setOpaque(false);

    renderPane.setPreferredSize( new Dimension(PWIDTH, PHEIGHT));

    canvas = makeCanvas(isCrossed);
    renderPane.add("Center", canvas);

    canvas.setFocusable(true);
    canvas.requestFocus();    // the canvas now has focus, so receives key events

    // detect window resizes, and reshape and center the canvas
    renderPane.addComponentListener( new ComponentAdapter() {
      public void componentResized(ComponentEvent evt)
      {  Dimension d = evt.getComponent().getSize();
         canvas.reshape(d.width, d.height);
         centerWindow();
      } // end of componentResized()
    });

    return renderPane;
  }  // end of makeRenderPanel()


  private StereoCanvasGL makeCanvas(boolean isCrossed)
  {
    // get a configuration suitable for an AWT Canvas (for StereoCanvasGL)
    GLCapabilities caps = new GLCapabilities();

    AWTGraphicsDevice dev = new AWTGraphicsDevice(null);
    AWTGraphicsConfiguration awtConfig = (AWTGraphicsConfiguration)
       GLDrawableFactory.getFactory().chooseGraphicsConfiguration(caps, null, dev);

    GraphicsConfiguration config = null;
    if (awtConfig != null)
      config = awtConfig.getGraphicsConfiguration();

    long period = (long) 1000.0/DEFAULT_FPS;
    // System.out.println("period: " + period + " ms");

    return new StereoCanvasGL(period*1000000L, PWIDTH, PHEIGHT, config, caps, isCrossed);
                              // ms --> nanosecs
  } // end of makeCanvas()


  // ----------------- window listener methods -------------

  public void windowActivated(WindowEvent e)
  { canvas.resumeGame();  }

  public void windowDeactivated(WindowEvent e)
  {  canvas.pauseGame();  }

  public void windowDeiconified(WindowEvent e)
  {  canvas.resumeGame();  }

  public void windowIconified(WindowEvent e)
  {  canvas.pauseGame(); }

  public void windowClosing(WindowEvent e)
  {  canvas.stopGame();  }

  public void windowClosed(WindowEvent e) {}
  public void windowOpened(WindowEvent e) {}

// -----------------------------------------

  public static void main_old(String[] args)
  {
    boolean isCrossed = false;

    if (args.length == 1) {
      if (args[0].toLowerCase().trim().equals("-x"))
        isCrossed = true;
    }
    else if (args.length > 1)
      System.out.println("Usage: runGL StereoGL [-x]");

    new StereoGL(isCrossed);
  } // end of main()


} // end of StereoGL class
